﻿using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading.Tasks;


namespace BuyWithoutLose.Controllers
{
    [AllowAnonymous]
    public class DingDanController : Controller
    {
		private readonly ILogger _logger;
		private readonly IRepository<Order, int> _orderRepostitory;
        private readonly IRepository<User, int> _userRepostitory;
        private readonly IRepository<OrderDetails, int> _orderDetailsRepostitory;
        private readonly IRepository<Product, int> _productRepostitory;
        private readonly IRepository<ProductPic, int> _productPicRepostitory;
        private readonly IRepository<UserAddress, int> _userAddressRepostitory;
        private readonly BuyWithoutLoseMoneyDBContext db;

        public DingDanController(ILogger<DingDanController> logger
            ,IRepository<Order, int> orderRepostitory
            , IRepository<User, int> userRepostitory,
            BuyWithoutLoseMoneyDBContext buyWithoutLoseMoneyDBContext,
            IRepository<OrderDetails, int> orderDetailsRepostitory,
            IRepository<ProductPic, int> productPicRepostitory,
            IRepository<Product, int> productRepostitory,
            IRepository<UserAddress, int> userAddressRepostitory)
        {
            _logger = logger;
            _orderRepostitory = orderRepostitory;
            _userRepostitory = userRepostitory;
            db = buyWithoutLoseMoneyDBContext;
            _orderDetailsRepostitory = orderDetailsRepostitory;
            _productPicRepostitory = productPicRepostitory;
            _productRepostitory = productRepostitory;
            _userAddressRepostitory = userAddressRepostitory;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                //这里通过 HttpContext.User.Claims 可以将我们在Login这个Action中存储到cookie中的所有
                //claims键值对都读出来，比如我们刚才定义的UserName的值Wangdacui就在这里读取出来了
                var userName = HttpContext.User.Claims.First().ValueType;
                var id = int.Parse(userName);
                var user = await _userRepostitory.GetAllListAsync();
                ViewBag.User = user;
                var orderDetails = await _orderDetailsRepostitory.GetAllListAsync();
                ViewBag.OrderDetails = orderDetails;
                var productPic = await _productPicRepostitory.GetAllListAsync();
                ViewBag.ProductPic = productPic;
                var userAddress=await _userAddressRepostitory.GetAllListAsync();
                ViewBag.UserAddress=userAddress;
                var order = _orderRepostitory.GetAllList().Where(x => x.UserId == id);
                return View(order);
            }
            else
            {
                return Redirect("/login/Login");
            }
        }

        #region 删除
        public async Task<IActionResult> Del(int id)
        {
            Order n = new Order() { Id = id };
            if (ModelState.IsValid)
            {
                await _orderRepostitory.DeleteAsync(n);
                return RedirectToAction("Index");
            }
            return Content("删除失败");
        }
        #endregion
    }
}
